package com.seed.application.validator;

import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.row.DbChain;
import com.mybatisflex.core.row.Row;
import com.seed.core.column.Column;
import com.seed.core.pojo.Const;
import com.seed.core.pojo.seed.Seed;
import com.seed.util.ThrowKit;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import static com.mybatisflex.core.query.QueryMethods.count;

public class SaveValidator {

    public static void validate(Seed seed, List<Row> rows) {
        for(Row row: rows) validate(seed, row);
    }

    public static void validate(Seed seed, Row row) {
        Map<String, Column> columns = seed.getColumns();
        for(Map.Entry<String, Column> entry: columns.entrySet()) {
            String columnName = entry.getKey();
            if(Const.ID.equals(columnName)) continue;

            Column column = entry.getValue();
            validateRequired(column, row);
            validateLength(column, row);
            validateUnique(seed, column, row);
            validateMaxMin(column, row);
        }
    }

    public static void validateRequired(Column column, Row row) {
        Boolean required = column.getRequired();
        String columnName = column.getName();
        if(required == null || !required) return;

        String fieldName = StrUtil.toCamelCase(columnName);
        Object value = row.get(fieldName);

//        RuntimeException e = new RuntimeException(String.format("【%s】不能为空", column.getLabel()));
//        if(value == null) throw e;
//
//        if(value instanceof String s && StrUtil.isBlank(s)) throw e;
    }

    public static void validateLength(Column column, Row row) {
        Integer length = column.getLength();
        if(length == null || length <= 0) return;

        String columnName = column.getName();
        String fieldName = StrUtil.toCamelCase(columnName);
        Object value = row.get(fieldName);
        if(value == null) return;

        String label = column.getLabel();
        if(((String) value).length() != length) ThrowKit.exception("【%s】长度必需等于【%s】", label, length);
    }

    public static void validateUnique(Seed seed, Column column, Row row) {
        Boolean unique = column.getUnique();
        if(unique == null || !unique) return;

        String columnName = column.getName();
        String fieldName = StrUtil.toCamelCase(columnName);
        Object value = row.get(fieldName);
        if(value == null) return;

        String tableName = seed.getTableName();
        int count = (int) DbChain.table(tableName)
                .select(count())
                .where(new QueryColumn(columnName).eq(value))
                .obj();
        if(count > 0) ThrowKit.exception("%s【%s】已存在", column.getLabel(), value);
    }

    public static void validateMaxMin(Column column, Row row) {
//        Object min = column.getMin();
//        Object max = column.getMax();
//        if(min == null && max == null) return;
//
//        String columnName = column.getName();
//        String fieldName = StrUtil.toCamelCase(columnName);
//        Object value = row.get(fieldName);
//        if(value == null) return;
//
//        String label = column.getLabel();
//        if(min == null) {
//            if(value instanceof String s) {
//                if(s.length() > (Integer) max) {
//                    throw new RuntimeException(String.format("【%s】长度不能大于【%s】", label, max));
//                }
//            } else if (value instanceof Long val) {
//                if(val > (Long) max) {
//                    throw new RuntimeException(String.format("【%s】不能大于【%s】", label, max));
//                }
//            } else if (value instanceof Double val) {
//                if(val > (Double) max) {
//                    throw new RuntimeException(String.format("【%s】不能大于【%s】", label, max));
//                }
//            } else if (value instanceof BigDecimal val) {
//                if(val.compareTo(new BigDecimal((String) max)) > 0) {
//                    throw new RuntimeException(String.format("【%s】不能大于【%s】", label, max));
//                }
//            }
//        } else if(max == null) {
//            if(value instanceof String s) {
//                if(s.length() < (Integer) min) {
//                    throw new RuntimeException(String.format("【%s】长度不能小于【%s】", label, min));
//                }
//            } else if (value instanceof Long val) {
//                if(val < (Long) min) {
//                    throw new RuntimeException(String.format("【%s】不能小于【%s】", label, min));
//                }
//            } else if (value instanceof Double val) {
//                if(val < (Double) min) {
//                    throw new RuntimeException(String.format("【%s】不能小于【%s】", label, min));
//                }
//            } else if (value instanceof BigDecimal val) {
//                if(val.compareTo(new BigDecimal((String) min)) < 0) {
//                    throw new RuntimeException(String.format("【%s】不能小于【%s】", label, min));
//                }
//            }
//        } else {
//            if(value instanceof String s) {
//                int len = s.length();
//                if(len < (Integer) min || len > (Integer) max) {
//                    throw new RuntimeException(String.format("【%s】长度必需介于【%s】和【%s】之间", label, min, max));
//                }
//            } else if (value instanceof Long val) {
//                if(val < (Long) min || val > (Long) max) {
//                    throw new RuntimeException(String.format("【%s】只能界于【%s】和【%s】之间", label, min, max));
//                }
//            } else if (value instanceof Double val) {
//                if(val < (Double) min || val > (Double) max) {
//                    throw new RuntimeException(String.format("【%s】只能界于【%s】和【%s】之间", label, min, max));
//                }
//            } else if (value instanceof BigDecimal val) {
//                BigDecimal ma = new BigDecimal((String) max);
//                BigDecimal mi = new BigDecimal((String) min);
//                if(val.compareTo(mi) < 0 || val.compareTo(ma) > 0) {
//                    throw new RuntimeException(String.format("【%s】只能界于【%s】和【%s】之间", label, min, max));
//                }
//            }
//        }
    }
}
